import time
from threading import Thread

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium import webdriver
from selenium.webdriver import ActionChains

# 打开浏览器
# BROWSER = webdriver.Chrome()
# BROWSER.set_window_size(1600, 900)
# 隐式等待
# WAIT = WebDriverWait(BROWSER, 5)


def get_page(url, js_str):
    """获取网页"""

    time_start = time.time()

    browser = webdriver.Chrome()
    browser.set_window_size(1600, 900)
    wait = WebDriverWait(browser, 5)

    browser.get(url)

    # time.sleep(4)

    # 打开登录界面
    login_el = browser.find_element_by_class_name('header-avatar')
    # print(login_el.get_attribute('outerHTML'))
    ActionChains(browser).move_to_element(login_el).perform()
    ActionChains(browser).click().perform()

    # 点击同意按钮 (这个破节点是真的难取)
    agree_el = browser.find_element_by_css_selector(
        'a[style="float: left;display: inline-block;width: 50%;height: 60px;line-height: 60px;'
        'font-size: 18px;background-color: #E33535;color: #fff;'
        'text-align: center;text-decoration: none;"]')
    agree_el.click()

    # BROWSER.execute_script(js_str)

    # 输入数据
    input_el = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[type="text"]')))
    input_el.clear()
    # FIXME 注意：跑的时候将此处替换成自己的账号密码！！！
    input_el.send_keys('账号\t密码\n')

    # 暴力穷举破解
    is_success = flash(input_el, js_str, browser)
    if is_success:
        print('成功！时间：', (time.time() - time_start))
    else:
        print('失败！时间：', (time.time() - time_start))


def flash(input_el, js_str, browser):
    """反复提交答案，直到成功为止"""

    for _ in range(1000):
        # print(_)
        browser.execute_script(js_str)
        input_el.send_keys('\n')

        login_el = browser.find_element_by_css_selector('.header_login.hover>a')

        if login_el.get_attribute('href') == 'javascript:void(0);':
            continue
        else:
            # print(js_str)
            return True


def main(js_str):
    """主函数"""

    url = 'http://www.1kkk.com/'
    get_page(url, js_str)

    # 关闭浏览器
    # time.sleep(2)
    # BROWSER.close()


if __name__ == '__main__':
    # main()

    # 按理说不应该在这里写线程代码的，但是本来想着瞎jb暴力测试一下，没想到成功率还挺高，那就保留了
    # TODO 重构代码！！！乱的跟啥一样！！！
    # 懒得重构代码了，反正没人看，自己懂就行[手动滑稽]

    # 开多个线程暴力穷举破解
    js_str1 = 'document.getElementsByClassName("rotate-background")[0].children[0].value=2;'
    js_str2 = 'document.getElementsByClassName("rotate-background")[1].children[0].value=2;'
    js_str3 = 'document.getElementsByClassName("rotate-background")[2].children[0].value=3;'
    js_str4 = 'document.getElementsByClassName("rotate-background")[3].children[0].value=1;'
    js_str5 = 'document.getElementsByClassName("rotate-background")[1].children[0].value=3;'
    js_str6 = 'document.getElementsByClassName("rotate-background")[1].children[0].value=3;'
    js_str7 = 'document.getElementsByClassName("rotate-background")[2].children[0].value=2;'

    t1 = Thread(target=main, args=(js_str1,))
    t2 = Thread(target=main, args=(js_str2,))
    t3 = Thread(target=main, args=(js_str3,))
    t4 = Thread(target=main, args=(js_str4,))
    t5 = Thread(target=main, args=(js_str5,))
    t6 = Thread(target=main, args=(js_str6,))
    t7 = Thread(target=main, args=(js_str7,))
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t5.start()
    t6.start()
    t7.start()
